#!/usr/bin/python
import subprocess,os,re,datetime,sys,linecache,codecs,chardet,time


# 执行batch脚本
def run_batch(filename):
    os.system(filename)

# 执行cmd命令
def exec_cmd(cmd):
    # cmd = 'cmd.exe d:/start.bat'
    p = subprocess.Popen("cmd.exe /c" + cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    curline = p.stdout.readline()
    while (curline != b''):
        print(curline)
        curline = p.stdout.readline()
    p.wait()
    if p.returncode != 0: #执行失败
        print ("exec cmd error: "+cmd+"\n")
        return False

# 清空文件
def clear_file(filename):
    f = open(filename, 'w')
    f.truncate()
    f.close()

# 读取文件内容
def readfile(filename):
    f = open(filename, 'r')
    content = f.read()
    f.close()
    return content

# 读取字符
def read_char(filename):
    f = open(filename, 'rb')
    content = f.read()
    f.close()
    return content

# 覆盖写入文件内容
def writefile(filename,content):
    f = open(filename, 'w')
    f.write(content)
    f.close()

# 追加写入文件内容
def appendfile(filename,content):
    f = open(filename, 'a')
    f.write(content)
    f.close()

# 读取指定行数据
def read_line_num(filename,num):
    f = open(filename, 'r')
    content = f.readlines()
    f.close()
    return content[num]

# 获取文件行数
def get_line_num(filename):
    f = open(filename, 'r')
    content = f.readlines()
    f.close()
    return len(content)

# 截取字符串
def split_line(filename,num):
    f = open(filename, 'r')
    content = f.readline()
    f.close()
    content = content.split(num)
    return content

# 替换文本内容
def replace(filename,old_str,new_str):
    f = open(filename, 'r')
    content = f.read()
    f.close()
    content = content.replace(old_str,new_str)
    f = open(filename, 'w')
    f.write(content)
    f.close()

# 创建目录
def mkdir(path):
    path=path.strip()
    path=path.rstrip("\\")
    isExists=os.path.exists(path) #查询目录是否存在
    if not isExists:
        os.makedirs(path) 
        print (path+' 创建成功')
        return True
    else:
        print (path+' 目录已存在')
        return False

# 删除目录
def del_dir(path):
    if os.path.isdir(path):
        for i in os.listdir(path):
            path_file = os.path.join(path,i)
            if os.path.isfile(path_file):
                os.remove(path_file)
            else:
                del_dir(path_file)
        os.rmdir(path)

# 等待毫秒
def wait_ms(ms):
    time.sleep(ms/1000.0)

# 删除空行
def del_blank_line(filename):
    f = open(filename, 'r')
    content = f.readlines()
    f.close()
    f = open(filename, 'w')
    for i in content:
        if i.strip():
            f.write(i)
    f.close()


if __name__ == '__main__':
    ssid='Ethan'
    password='88888888'
    path = os.getcwd()  # 获取当前路径

    exec_cmd('adb wait-for-device')
    exec_cmd('adb root')

    for data in range(0x3A,0x3D): # 0x3D-1=0x3C
        print("data is " + hex(data))
        print('开启wifi')
        exec_cmd('adb shell ifconfig wlan0 up && adb shell svc wifi enable') # 开启wifi
        wait_ms(500)
        print('CMD command: adb shell iwpriv wlan0 driver \"set_mcr 0x78001034 ' + hex(data) +'\"')
        exec_cmd('adb shell iwpriv wlan0 driver \"set_mcr 0x78001034 ' + hex(data) +'\"') # 设置wifi参数
        wait_ms(500)
        # 进入wifi设置界面
        exec_cmd('adb shell am start -a android.settings.WIFI_SETTINGS') 
        wait_ms(2000)
        # 下滑底部
        exec_cmd('adb shell input swipe 1350 1000 1350 300')
        exec_cmd('adb shell input swipe 1350 1000 1350 300')
        exec_cmd('adb shell input swipe 1350 1000 1350 300')
        exec_cmd('adb shell input swipe 1350 1000 1350 300')
        # 添加网络
        exec_cmd('adb shell input tap 1350 710')
        # 输入ssid
        exec_cmd('adb shell input text ' + ssid)
        # 设置安全性
        exec_cmd('adb shell input tap 1350 320')
        exec_cmd('adb shell input tap 1350 300')
        # 输入密码并保存
        exec_cmd('adb shell input tap 1350 440')
        exec_cmd('adb shell input text ' + password)
        exec_cmd('adb shell input tap 1920 520')
        wait_ms(500)
        # 上滑顶部
        exec_cmd('adb shell input swipe 1350 300 1350 1000')
        exec_cmd('adb shell input swipe 1350 300 1350 1000')
        exec_cmd('adb shell input swipe 1350 300 1350 1000')
        exec_cmd('adb shell input swipe 1350 300 1350 1000')
        # # 点击连接
        # exec_cmd('adb shell input tap 1910 510')
        wait_ms(25000) # 等待连接
        exec_cmd('adb shell svc wifi disable')
        wait_ms(500)
        data=data+1
        
        


